﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using QxFramework.Utilities;
using QxFramework.Core;


namespace QxFramework.Core
{

    /// <summary>
    /// 点击后触发的消息
    /// </summary>
    public enum TapMsg
    {
        TapDown,
        TapUp,
        TapMove,
        Escape
    }



    public class InputManager : MonoSingleton<InputManager>
    {


        private MessageQueue<TapMsg> _queue;
        private Camera _camera;
    
        //无论是鼠标还是触摸，所得到的屏幕空间的坐标值
        private Vector3 _tapTargetPos;
        public Vector3 TapTarget
        {
            get
            {
                return _tapTargetPos;
            }
        }

        //最后一帧的位移量
        private float _deltaPosX;
        public float DeltaPosX
        {
            get
            {
                return _deltaPosX;
            }
        }

        private float _deltaPosY;
        public float DeltaPosY
        {
            get
            {
                return _deltaPosY;
            }
        }

        /// <summary>
        /// 缩放
        /// </summary>
        public float ScaleOffest
        {
            get
            {
                return _scaleOffest;
            }
        }

        private float _scaleOffest;

        public bool IsDoubleTapping
        {
            get
            {
                return _isDoubleTapping;
            }
        }
        private bool _isDoubleTapping;

        public int TappingCount
        {
            get
            {
                return Input.touchCount;
            }
        }

        private void Start()
        {
            _queue = MessageManager.Instance.Get<TapMsg>();
            _camera = Camera.main;
        }
        

        
        void Update()
        {
//#if UNITY_STANDALONE_WIN || UNITY_EDITOR
            _scaleOffest = Input.GetAxis("Mouse ScrollWheel");

            if (Input.GetMouseButtonDown(0))
            {
                //发消息
                _queue.DispatchMessage(TapMsg.TapDown, _camera.ScreenToWorldPoint( Input.mousePosition));
                //Debug.Log("SendTapDown");
            }

            if (Input.GetMouseButton(0))
            {
                
                if (Input.mousePosition != _tapTargetPos)
                {
                    //发消息,移动的时候才发
                    _queue.DispatchMessage(TapMsg.TapMove, _camera.ScreenToWorldPoint(Input.mousePosition));
                    //Debug.Log("SendTapMove");
                }
                _tapTargetPos = _camera.ScreenToWorldPoint(Input.mousePosition);
                _deltaPosX = Input.GetAxis("Mouse X");
                _deltaPosY = Input.GetAxis("Mouse Y");
                Debug.Log(_tapTargetPos);
            }
            else
            {

                _tapTargetPos = Vector3.zero;
                _deltaPosX = 0;
                _deltaPosY = 0;
            }

            if (Input.GetMouseButtonUp(0))
            {
                //发消息
                _queue.DispatchMessage(TapMsg.TapUp,_camera.ScreenToWorldPoint(Input.mousePosition));
                //Debug.Log("SendTapUp");
            }

            if(Input.GetKeyDown(KeyCode.Escape))
            {
                _queue.DispatchMessage(TapMsg.Escape, this);
            }

            
            //#endif

#if UNITY_ANDROID && !UNITY_EDITOR

            //以下为安卓端
            if (Input.touchCount <= 0)
            {
                _tapTargetPos = Vector3.zero;
                _deltaPosX = 0;
                _deltaPosY = 0;
            }

            if (Input.touchCount == 1) //单点触碰
            {
                _tapTargetPos = _camera.ScreenToWorldPoint(Input.GetTouch(0).position);
                _deltaPosX = Input.GetTouch(0).deltaPosition.x/20;
                _deltaPosY = Input.GetTouch(0).deltaPosition.y/20;
                switch(Input.touches[0].phase)
                {
                    case TouchPhase.Began:
                        _queue.DispatchMessage(TapMsg.TapDown, Input.GetTouch(0).position);
                        break;
                    case TouchPhase.Moved:
                        _queue.DispatchMessage(TapMsg.TapMove, Input.GetTouch(0).position);
                        break;
                    case TouchPhase.Ended:
                        _queue.DispatchMessage(TapMsg.TapUp, Input.GetTouch(0).position);
                        break;

                    default:
                        break;
                }
            }
            if(Input.touchCount == 2)
            {
                _isDoubleTapping = true;
            }
            else
            {
                _isDoubleTapping = false;
            }

            
            if ((Input.touchCount == 2) && (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved))
            {
                _tapTargetPos = Vector3.zero;
                _deltaPosX = 0;
                _deltaPosY = 0;

                Touch touch1 = Input.GetTouch(0);
                Touch touch2 = Input.GetTouch(1);

                _scaleOffest = Vector2.Distance(touch1.position + touch1.deltaPosition, touch2.deltaPosition + touch2.position)
                    - Vector2.Distance(touch1.position, touch2.position);
                //_scaleOffest = Mathf.Abs(_scaleOffest) > 100f ? _scaleOffest : 0;
            }
            else
            {
                _scaleOffest = 0f;
            }
#endif
        }





    }
}
